<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>FAQ &#8212; Boost.HigherOrderFunctions 0.6 documentation</title>
    
    <link rel="stylesheet" href="../../_static/boostbook.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../',
        VERSION:     '0.6',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true,
        SOURCELINK_SUFFIX: '.txt'
      };
    </script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" />
    <link rel="next" title="Acknowledgements" href="acknowledgements.html" />
    <link rel="prev" title="Partial function evaluation" href="partialfunctions.html" /> 
  </head>
  <body role="document">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86"src="../../_static/boost.png"></td>
</tr></table>

<div class="spirit-nav">
<a accesskey="p"  href="partialfunctions.html"><img src="../../_static/prev.png" alt="Prev"></a>
<a accesskey="u" href="discussion.html"><img src="../../_static/up.png" alt="Up"></a>
<a accesskey="h" href="../index.html"><img src="../../_static/home.png" alt="Home"></a>
<a accesskey="n"  href="acknowledgements.html"><img src="../../_static/next.png" alt="Next"></a>
</div>
  

    <div class="document">
  <div class="chapter">
      <div class="body" role="main">
        
  <!-- Copyright 2018 Paul Fultz II
     Distributed under the Boost Software License, Version 1.0.
     (http://www.boost.org/LICENSE_1_0.txt)
--><div class="section" id="faq">
<h1>FAQ<a class="headerlink" href="#faq" title="Permalink to this headline">¶</a></h1>
<div class="section" id="q-why-is-const-required-for-the-call-operator-on-function-objects">
<h2>Q: Why is <code class="docutils literal"><span class="pre">const</span></code> required for the call operator on function objects?<a class="headerlink" href="#q-why-is-const-required-for-the-call-operator-on-function-objects" title="Permalink to this headline">¶</a></h2>
<p>Mutable function objects are not prohibited, they just need to be explicit by
using the adaptor <a class="reference internal" href="../../include/boost/hof/mutable.html"><span class="doc">mutable</span></a>. The main reason for this, is that it can lead to
many suprising behaviours. Many times function objects are copied by value
everywhere. For example,</p>
<div class="highlight-cpp"><div class="highlight"><pre><span class="k">struct</span> <span class="n">counter</span>
<span class="p">{</span>
    <span class="kt">int</span> <span class="n">i</span><span class="p">;</span>
    <span class="n">counter</span><span class="p">()</span> <span class="o">:</span> <span class="n">i</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
    <span class="p">{}</span>

    <span class="k">template</span><span class="o">&lt;</span><span class="n">class</span><span class="p">...</span> <span class="n">Ts</span><span class="o">&gt;</span>
    <span class="kt">int</span> <span class="k">operator</span><span class="p">()(</span><span class="n">Ts</span><span class="o">&amp;&amp;</span><span class="p">...)</span>
    <span class="p">{</span>
        <span class="k">return</span> <span class="n">i</span><span class="o">++</span><span class="p">;</span>
    <span class="p">}</span>
<span class="p">};</span>


<span class="n">counter</span> <span class="n">c</span><span class="p">{};</span>
<span class="n">proj</span><span class="p">(</span><span class="n">mutable_</span><span class="p">(</span><span class="n">c</span><span class="p">))(</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">);</span>
<span class="c1">// Prints 0, not 2</span>
<span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">c</span><span class="p">.</span><span class="n">i</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
</pre></div>
</div>
<p>The example won&#8217;t ever yield the expected value, because the function mutates
a copy of the objects. Instead, <code class="docutils literal"><span class="pre">std::ref</span></code> should be used:</p>
<div class="highlight-cpp"><div class="highlight"><pre><span class="n">counter</span> <span class="n">c</span><span class="p">{};</span>
<span class="n">proj</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">ref</span><span class="p">(</span><span class="n">c</span><span class="p">))(</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">);</span>
<span class="c1">// Prints 2</span>
<span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">c</span><span class="p">.</span><span class="n">i</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
</pre></div>
</div>
<p>Which will print the expected value.</p>
<p>Another reason why <code class="docutils literal"><span class="pre">const</span></code> is required is because of supporting <code class="docutils literal"><span class="pre">constexpr</span></code> on
C++11 compilers. In C++11, <code class="docutils literal"><span class="pre">constexpr</span></code> implies <code class="docutils literal"><span class="pre">const</span></code>, so it would be
impossible to provide a non-const overload for functions that is <code class="docutils literal"><span class="pre">constexpr</span></code>.
Instead, <code class="docutils literal"><span class="pre">constexpr</span></code> would have to be made explicit. Considering the pitfalls
of mutable function objects, it would be better to make mutability explicit
rather than <code class="docutils literal"><span class="pre">constexpr</span></code>.</p>
</div>
<div class="section" id="q-is-the-reinterpret-cast-in-boost-hof-static-lambda-undefined-behaviour">
<h2>Q: Is the reinterpret cast in BOOST_HOF_STATIC_LAMBDA undefined behaviour?<a class="headerlink" href="#q-is-the-reinterpret-cast-in-boost-hof-static-lambda-undefined-behaviour" title="Permalink to this headline">¶</a></h2>
<p>Not really, since the objects are empty, there is no data access. There is a
static assert to guard against this restriction.</p>
<p>Now there could be an insane implementation where this doesn&#8217;t work(perhaps
the lambdas are not empty for some strange reason), which the library would
have to apply a different technique to make it work. However, this is quite
unlikely considering that C++ will probably get constexpr lambdas and inline
variables in the future.</p>
<p>Alternatively, the factory pattern can be used instead of
<a class="reference internal" href="../../include/boost/hof/lambda.html#boost-hof-static-lambda-function"><span class="std std-ref">BOOST_HOF_STATIC_LAMBDA_FUNCTION</span></a>, which doesn&#8217;t require an reinterpret cast:</p>
<div class="highlight-cpp"><div class="highlight"><pre><span class="k">struct</span> <span class="n">sum_factory</span>
<span class="p">{</span>
    <span class="k">auto</span> <span class="k">operator</span><span class="o">*</span><span class="p">()</span> <span class="k">const</span>
    <span class="p">{</span>
        <span class="k">return</span> <span class="p">[](</span><span class="k">auto</span> <span class="n">x</span><span class="p">,</span> <span class="k">auto</span> <span class="n">y</span><span class="p">)</span>
        <span class="p">{</span>
            <span class="k">return</span> <span class="n">x</span> <span class="o">+</span> <span class="n">y</span><span class="p">;</span>
        <span class="p">};</span>
    <span class="p">}</span>
<span class="p">}</span>

<span class="n">BOOST_HOF_STATIC_FUNCTION</span><span class="p">(</span><span class="n">sum</span><span class="p">)</span> <span class="o">=</span> <span class="n">boost</span><span class="o">::</span><span class="n">hof</span><span class="o">::</span><span class="n">indirect</span><span class="p">(</span><span class="n">sum_factory</span><span class="p">{});</span>
</pre></div>
</div>
</div>
</div>


      </div>
  </div>
      <div class="clearer"></div>
    </div>
    <div class="footer" role="contentinfo">
    <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
    <td align="left"></td>
    <td align="right"><div class="copyright-footer">
            &#169; Copyright 2016, Paul Fultz II.
        
          Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.5.6.
          <p>Distributed under the Boost Software License, Version 1.0.
          (See accompanying file <code class="filename">LICENSE_1_0.txt</code> or copy at 
          <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
          </p>
    </div></td>
    </tr></table>
    </div>
  </body>
</html>